package com.zqxx.examine.management.controller.page.teacher;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.common.utils.PagingUtil;
import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.QuestionService;
import com.zqxx.examine.management.service.UserService;
import com.zqxx.examine.model.question.Field;
import com.zqxx.examine.model.user.Group;
import com.zqxx.examine.model.user.User;

@Controller
public class UserPageTeacher
{
  @Autowired
  private UserService userService;
  @Autowired
  private QuestionService questionService;
  
  @RequestMapping(value={"teacher/user/student-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String userListPage(Model model, HttpServletRequest request)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    int index = 1;
    if (request.getParameter("page") != null) {
      index = Integer.parseInt(request.getParameter("page"));
    }
    Page<User> page = new Page();
    page.setPageNo(index);
    page.setPageSize(20);
    int groupId = 0;
    String searchStr = "";
    if (request.getParameter("groupId") != null)
    {
      groupId = Integer.parseInt(request.getParameter("groupId"));
      searchStr = request.getParameter("searchStr");
    }
    List<User> userList = this.userService.getUserListByGroupIdAndParams(groupId, "ROLE_STUDENT", searchStr, page);
    String pageStr = PagingUtil.getPagelink(index, page.getTotalPage(), "", "admin/user-list");
    List<Group> groupList = this.userService.getGroupListByUserId(userInfo.getUserid(), null);
    model.addAttribute("groupId", Integer.valueOf(groupId));
    model.addAttribute("userList", userList);
    model.addAttribute("pageStr", pageStr);
    model.addAttribute("groupList", groupList);
    return "user-list";
  }
  
  @RequestMapping(value={"teacher/user/student-examhistory"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String examHistoryPage(Model model, HttpServletRequest request)
  {
    return "";
  }
  
  @RequestMapping(value={"teacher/user/student-profile"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String studentProfilePage(Model model, HttpServletRequest request)
  {
    return "";
  }
  
  @RequestMapping(value={"teacher/user/inner/user-list/{groupId}-{authority}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String showUserListInnerAdminPage(Model model, HttpServletRequest request, @PathVariable Integer groupId, @PathVariable String authority)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    int index = 1;
    if (request.getParameter("page") != null) {
      index = Integer.parseInt(request.getParameter("page"));
    }
    Page<User> page = new Page();
    page.setPageNo(index);
    page.setPageSize(15);
    String searchStr = "";
    if (request.getParameter("searchStr") != null) {
      searchStr = request.getParameter("searchStr");
    }
    List<User> userList = this.userService.getUserListByGroupIdAndParams(groupId.intValue(), authority, searchStr, page);
    String pageStr = PagingUtil.getPagelink(index, page.getTotalPage(), "&searchStr=" + searchStr, "teacher/user/inner/user-list/" + groupId + "-" + authority);
    
    List<Group> groupList = this.userService.getGroupListByUserId(userInfo.getUserid(), null);
    model.addAttribute("userList", userList);
    model.addAttribute("pageStr", pageStr);
    model.addAttribute("groupList", groupList);
    model.addAttribute("groupId", groupId);
    model.addAttribute("authority", "ROLE_STUDENT");
    model.addAttribute("searchStr", searchStr);
    return "inner/user-list";
  }
  
  @RequestMapping(value={"/teacher/user/add-user"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String addUserPage(Model model, HttpServletRequest request)
  {
    List<Field> fieldList = this.questionService.getAllField(null);
    model.addAttribute("fieldList", fieldList);
    return "add-user";
  }
}